/*
 * @lc app=leetcode.cn id=661 lang=typescript
 *
 * [661] 图片平滑器
 */

// @lc code=start

let cor = (x: number, y: number) => [
    [x - 1, y],
    [x + 1, y],
    [x - 1, y - 1],
    [x + 1, y - 1],
    [x - 1, y + 1],
    [x + 1, y + 1],
    [x, y + 1],
    [x, y - 1],
];
const avg = (args: number[]) => {
    return Math.floor(args.reduce((a, b) => a + b, 0) / args.length);
};
function imageSmoother(img: number[][]): number[][] {
    return img.map((row, y) => {
        return row.map((i, x) => {
            const around = cor(x, y)
                .map(([x1, y1]) => {
                    return img?.[y1]?.[x1];
                })
                .filter((i) => typeof i === "number");
            return avg([i, ...around]);
        });
    });
}
// @lc code=end
